package oleg.lcdui;

/**
 * Copyright 2008 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.microedition.lcdui.*;
import java.util.*;

public class TextListItem extends CustomItem
{
  private final static int SPACE_INPUT_TEXT_AND_LIST = 0;
  private final static int SPACE_LIST_ITEMS = 0;

  private StringBuffer inputTextSB = new StringBuffer();
  private Vector elems = new Vector();
  private int selItem = -1;

  private Font font;

  // Keeps track of traverse whether it is inside or outside of the item
  private boolean traverseInside = false;

  public TextListItem(String label) {
    super(label);

    font = Font.getDefaultFont();

    // Do some artificial initialization
    inputTextSB.append("Input String");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
    append("one");
    append("two");
    append("three");
  }

  ////////////////////////////////////////
  // METHODS REQUIRED BY APPLICATION
  ////////////////////////////////////////
  public String getString() {
    return inputTextSB.toString();
  }

  public void deleteAll() {
    elems.removeAllElements();
    selItem = -1;

    invalidate();
  }

  public void append(String item) {
    elems.addElement(item);
    if (selItem == -1) {
      selItem = 0;
    }

    invalidate();
  }

  public int size() {
    return elems.size();
  }

  public int getSelectedIndex() {
    return selItem;
  }

  ////////////////////////////////////////
  // ABSTRACT METHODS INHERITED FROM CustomItem
  ////////////////////////////////////////

  protected int getMinContentHeight() {
    int height = 0;
    
    int charHeight = font.getHeight();

    // Add input text height
    height += charHeight;

    // Add space between input text and list
    height += SPACE_INPUT_TEXT_AND_LIST;

    // Add list height
    height += charHeight * elems.size();
    height += elems.size() == 0 ? 0 : (elems.size() - 1) * SPACE_LIST_ITEMS;

    return height;
  }

  protected int getMinContentWidth() {
    int width = font.stringWidth(getLabel());

    // Input text width
    width = Math.max(width, font.stringWidth(inputTextSB.toString()));

    // list items width
    for (int i = 0; i < elems.size(); i++) {
      String curElem = (String) elems.elementAt(i);
      width = Math.max(width, font.stringWidth(curElem));
    }

    return width;
  }

  protected int getPrefContentHeight(int width) {
    return getMinContentHeight();
  }

  protected int getPrefContentWidth(int height) {
    return getMinContentWidth();
  }

  protected void paint(Graphics g, int w, int h) {
    int translatedY = 0;
    int fgColor = g.getColor();
    int invertRed = 80;
    int invertBlue = 80;
    int invertGreen = 80;

    // Paint input string
    g.drawString(inputTextSB.toString(), 0, 0, Graphics.TOP|Graphics.LEFT);
    g.translate(0, font.getHeight());
    translatedY += font.getHeight();

    // Paint list items
    for (int i = 0; i < elems.size(); i++) {
      String curElem = (String) elems.elementAt(i);

      if (i == selItem) {
        g.setColor(invertRed, invertGreen, invertBlue);
        curElem = "->" + curElem;
      }
      g.drawString(curElem, 0, 0, Graphics.TOP|Graphics.LEFT);
      g.setColor(fgColor);

      g.translate(0, font.getHeight());
      translatedY += font.getHeight();
    }

    g.translate(0, -translatedY);
  }

//   protected void keyPressed(int keyCode) {
//     inputTextSB.delete(0, inputTextSB.length());
//     inputTextSB.append("p_" + keyCode + "_" + getGameAction(keyCode));
//     invalidate();
//   }

//   protected void keyReleased(int keyCode) {
//     inputTextSB.delete(0, inputTextSB.length());
//     inputTextSB.append("r_" + keyCode + "_" + getGameAction(keyCode));
//     invalidate();
//   }

  protected boolean traverse(int dir,
                             int viewportWidth,
                             int viewportHeight,
                             int[] visRect_inout)
  {
    System.out.println("traverseInside: " + traverseInside +
                       " dir: " + dir +
                  " viewportWidth: " +viewportWidth +
                  " viewportHeight: " + viewportHeight);

    if (!traverseInside) {
      // Initialize
      traverseInside = true;
      System.out.println("1: returning true" ); 
      return true;
    }


    if (dir == Canvas.UP) {
      if (selItem > 0) {
        selItem--;
        return true;
      }
    } 

    if (dir == Canvas.DOWN) {
      if (selItem < elems.size() - 1) {
        selItem++;
        return true;
      }
    }

    return false;

  }

  protected void traverseOut() {
    traverseInside = false;
  }
}
